package EGS.impl;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service502Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 出站检查 1、获取工位工序 2、查询条码本工序情况
	 */
	public EGS_execute_service502Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer;
			int iiLayer;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("502")) {
				iSN = Mroot.element("body").element("pcb").attribute("barcode").getValue();
				iLayer = Mroot.element("body").element("pcb").attribute("pcbSide").getValue();
				iiLayer = Integer.parseInt(iLayer);
				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				try {
					if (iiLayer != iClient.myLayer) {
						String str = "工单激活面别与产品上传面别不一致！";
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", str);
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(Integer.parseInt(msgClass + "0"), "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
						return MSG;
					}
					boolean result = iClient.checkWIPfinish(iSN);
//					System.out.println("" + iSN + "_" + iStation + "_" + iLayer + "_" + result);
					String errorCode, errorText, actionCode;
					if (result) {// 本工序已Pass
						errorCode = "0";
						errorText = "Valid";
						actionCode = "0";
					} else {// 本工序未做
						errorCode = "1";
						errorText = "本工序未执行";
						actionCode = "0";
					}
					eleR.addAttribute("errorCode", errorCode);
					eleR.addAttribute("errorText", errorText);
					eleR.addAttribute("actionCode", actionCode);
					AKSLogs(Integer.parseInt(msgClass + "1"),
							"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorCode);
				} catch (Exception e) {// 本工序Fail
					String errorCode, errorText, actionCode;
					if (e.getMessage().equals("NGfinish")) {
						errorCode = "2";
						errorText = "本工序Fail";
						actionCode = "0";
						AKSLogs(Integer.parseInt(msgClass + "1"),
								"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorCode);
					} else {
						String str = errCodeManagement.getErrText(e.getMessage());
						errorCode = "3";
						errorText = str;
						actionCode = "0";
						AKSLogs(Integer.parseInt(msgClass + "0"), "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
					}
					eleR.addAttribute("errorCode", errorCode);
					eleR.addAttribute("errorText", errorText);
					eleR.addAttribute("actionCode", actionCode);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
